package com.lcredu.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcredu.seckill.mapper.UserMapper;
import com.lcredu.seckill.pojo.User;
import com.lcredu.seckill.service.UserService;
import com.lcredu.seckill.util.CookieUtil;
import com.lcredu.seckill.util.MD5Util;
import com.lcredu.seckill.util.UUIDUtil;
import com.lcredu.seckill.util.response.RespBean;
import com.lcredu.seckill.util.response.RespBeanEnum;
import com.lcredu.seckill.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 黎长容
 * @date 2025-08-23 19:15
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate redisTemplate;

    //写一个登录的方法
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        //1. 判断vo里面的手机号是否为空
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

//        if (!StringUtils.hasText(mobile) || !StringUtils.hasText(password)) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//
//        //2. 判断手机号格式是否正确
//        if (!ValidatorUtil.isMobile(mobile)) {
//            return RespBean.error(RespBeanEnum.MOBILE_NO_MATCH);
//        }

        //3. 判断用户是否存在
        User user = userMapper.selectById(mobile);
        if (user == null) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
//        log.info("midPass={}", password);
//        log.info("用户的盐={}", user.getSlat());
        //4. 判断密码是否正确
        String dbPass = MD5Util.midPassToDBPass(password, user.getSlat());
//        log.info("dbPass={}", dbPass);
        if (!dbPass.equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //存储cookie和session
        String ticket = UUIDUtil.getUUID();

        //储存到Session
//        request.getSession().setAttribute(ticket, user);
        System.out.println("redisTemplate使用=" + redisTemplate.hashCode());
        redisTemplate.opsForValue().set("user:" + ticket, user);
        //储存到cookie
        CookieUtil.setCookie(request, response, "userTicket", ticket);


        return RespBean.success();
    }

    @Override
    public User getUser(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        //1.判断票据是否为空
        //2.判断人是否存在
        //3.刷新cookie
        if (!StringUtils.hasText(userTicket)) {
            return null;
        }

        //如果查询一次存在就刷新cookie
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user != null) {
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }

        return user;
    }

    @Override
    public void loginOutUser(Long userId, HttpServletRequest request, HttpServletResponse response) {
        String userTicket = CookieUtil.getCookieValue(request, "userTicket");
        String key = "user:" + userTicket;
        redisTemplate.delete(key);

        CookieUtil.deleteCookie(request, response, "userTicket");

    }

    /*
        1.修改数据库中的用户信息
            如果user对象为空就要返回错误信息--用户不存在
        2.删除redis中的用户信息
        
     */
    @Override
    public RespBean updateUserPassword(String ticket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUser(ticket, request, response);
        if (user == null) {
            return RespBean.error(RespBeanEnum.MOBILE_NOT_EXISTS);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password, user.getSlat()));
        int i = userMapper.updateById(user);

        if (i > 0) {
            //删除redis中的信息
            redisTemplate.delete("user:" + ticket);
            return RespBean.success();
        }


        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }

}
